<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<script type="text/javascript">
		document.body.onkeypress=function(){
			console.log(event.keyCode);
		}
	</script>
	<body>
		请输入数字：<input type="text" onkeypress="fun()" />
		<script type="text/javascript">
			function fun(){
				 // 0-9
				if(event.keyCode<48||event.keyCode>57){
					//alert("only number!!!");
					event.preventDefault();//取消默认操作，如果是非数字直接无法输入
				
					return;
				}
			}
		</script>
	</body>
</html>
